package com.jt.mapper;

import com.jt.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Repository
public class UserMapperImpl implements UserMapper{
    @Override
    public List<User> findAll() {
        //指定配置文件的根目录
        String resource = "mybaits-config.xml";
        //通过我们的io流加载配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //实例化工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SQLSession 通过SqlSession可以直接操作数据库
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取接口对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //获取接口的方法
        List<User> listUser = userMapper.findAll();
        //暂时手动关闭连接
        sqlSession.close();
        return listUser;
    }
}
